package com.manor.paradise.dialog

import android.content.Context
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.bumptech.glide.Glide
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.dialog_edoassword_popup.view.*

/**
 * 修改密码
 * @constructor
 */
@Suppress("UsePropertyAccessSyntax")
class EdPasswordDialog(context: Context) : FullScreenPopupView(context), View.OnClickListener {
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_edoassword_popup
    }

    override fun onCreate() {
        super.onCreate()
        im_dismiss.setOnClickListener(this)
        ed_oldmm.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_oldmm.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_oldmm.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框

        ed_newmm.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_newmm.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_newmm.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框


        ed_newmms.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_newmms.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_newmms.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框
        tv_confirm.setOnClickListener {
            if (!TextUtils.isEmpty(ed_oldmm.text.toString()) && !TextUtils.isEmpty(ed_newmm.text.toString()) && !TextUtils.isEmpty(ed_newmms.text.toString())) {
                if (!ed_newmms.text.toString().equals(ed_newmm.text.toString())) {
                    ToastUtil.show("新密码输入不一致")
                    return@setOnClickListener
                }
                ElvesHttpUtil.getEdpassword(ed_newmm.text.toString(), ed_oldmm.text.toString(), object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        ToastUtil.show(msg)
                        if (code != 200) {

                            return
                        }

                        dismiss()


                    }

                    override fun onError() {
                        super.onError()

                    }
                })
            } else {
                if (TextUtils.isEmpty(ed_oldmm.text.toString())) {
                    ToastUtil.show("请输入旧交易密码")
                }
                if (TextUtils.isEmpty(ed_newmm.text.toString())) {
                    ToastUtil.show("请输入新密码")
                }
                if (TextUtils.isEmpty(ed_newmms.text.toString())) {
                    ToastUtil.show("请确认新密码")
                }
            }

        }

    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    dismiss()
                }
            }
    }
    override fun onDestroy() {
        Glide.get(context).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_PASSWORD)
        super.onDestroy()
    }

}